package com.probiz.estore.common.model.system;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.system.base.RegionTbl;
public class Region extends RegionTbl {
	
	public final static Integer REGION_TYPE_COUNTRY = Integer.valueOf(1);
	public final static Integer	SORTORDER_DEFAULT_VALUE	= new Integer("9999");
  	/**
	 * Default Empty Constructor for class Region
	 */
	public Region () {
		super();
	}
	
	/**
	 * Default Key Fields Constructor for class Region
	 */
	public Region (
		 Integer in_regionId
		) {
		super (
		  in_regionId
		);
	}
	
	/**
     * 获取父目录完整的目录路径名 例：中国>广东>广州
	 * @return 父目录完整的目录路径名 
	 */
	public String getParentFullCategoryPathName(){
		StringBuffer categoryPathNameBuffer=new StringBuffer(100);
		Region parentRegion=this.getParent();
		while (parentRegion!=null && !parentRegion.getRegionId().equals(Constants.REGIONID_WORLD)) {
			categoryPathNameBuffer.insert(0, ">");
			categoryPathNameBuffer.insert(0, parentRegion.getRegionName());
			parentRegion=parentRegion.getParent();
		}
		String categoryPathName = categoryPathNameBuffer.toString();
		if(!StringUtils.isEmpty(categoryPathName))categoryPathName = categoryPathName.substring(0,categoryPathName.length()-1);
		return categoryPathName;
	}
	/**
	 * 获取当前地区完整名称,例如: 中国,广东,广州
	 * @return
	 */
	public String getFullName(){
		StringBuffer fullNameBuffer=new StringBuffer(100);
		Region parentRegion=this.getParent();
		while (parentRegion!=null && !parentRegion.getRegionId().equals(Constants.REGIONID_WORLD)) {
			fullNameBuffer.insert(0, ",");
			fullNameBuffer.insert(0, parentRegion.getRegionName());
			parentRegion=parentRegion.getParent();
		}
		fullNameBuffer.append(this.getRegionName());
		String fullName = fullNameBuffer.toString();
		return fullName;
	}
	/**
	 * 获取自身和祖辈的region列表，例如[中国，广东,广州]
	 * @return
	 */
	public List<Region> getParentsAndSelf(){
		List<Region> regionList = new ArrayList<Region>();
		regionList.add(this);
		Region parentRegion=this.getParent();
		while (parentRegion!=null && !parentRegion.getRegionId().equals(Constants.REGIONID_WORLD)) {
			regionList.add(parentRegion);
			parentRegion=parentRegion.getParent();
		}
		Collections.reverse(regionList);
		return regionList;
		
	}
}
